//
//  oneViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/4/12.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "oneViewController.h"
#import "configddd.h"
#import <Masonry/Masonry.h>
#import "UIImageView+WebCache.h"
#import "twoTableViewCell.h"
#import "threeTableViewCell.h"
#import "fourTableViewCell.h"
#import "payViewController.h"
#import "loginViewController.h"

@interface oneViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *orderTableView;

@end

@implementation oneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancleOrderNotification:) name:@"cancleOrder" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refundNotification:) name:@"refund" object:nil];
    
    [self isLogin];
    
    self.dataArray = [[NSMutableArray alloc]init];
    self.orderTableView = [[UITableView alloc]init];
}

- (void)isLogin{

    // 登陆判断
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Is_Login
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       [self requestData];
                                       [hud removeFromSuperview];
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       loginViewController *loginVc =[[loginViewController alloc]init];
                                       [self.navigationController presentViewController:loginVc animated:YES completion:^{}];
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

- (void)requestData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = NO;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Cooperation_Order
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       _dataArray = [returnData objectForKey:@"data"];
                                       
                                       NSLog(@"\n\n\n====dataArray====%@",_dataArray);
                                       [_orderTableView reloadData];
                                       [hud removeFromSuperview];
                                       
                                   }
                                   else{
                                       
                                       [hud removeFromSuperview];
                                       //[PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

#pragma mark ------------------ init ---------------------

- (void)setOrderTableView:(UITableView *)orderTableView{
    
    _orderTableView = orderTableView;
    _orderTableView.frame = CGRectMake(0, 0, scrWidth(), scrHeight() - screenScale(80) - 64);
    _orderTableView.backgroundColor = getColor(@"ffffff");
    _orderTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _orderTableView.separatorStyle=UITableViewCellSelectionStyleNone;
    _orderTableView.delegate = self;
    _orderTableView.dataSource = self;
    [self.view addSubview:_orderTableView];
}

#pragma mark -------------------- Notification -----------------------

- (void)cancleOrderNotification:(NSNotification *)text{
    
    if ([text.userInfo[@"selected"] isEqualToString:@"yes"]) {
        
        [self requestData];
    }
    
    if ([text.userInfo[@"selected"] isEqualToString:@"oneVC"]) {
        
        payViewController *payVC = [[payViewController alloc]init];
        payVC.str = [NSString stringWithFormat:@"%@",[_dataArray objectAtIndex:[text.userInfo[@"index"] intValue]][@"total"] ];
        payVC.number = [_dataArray objectAtIndex:[text.userInfo[@"index"] intValue]][@"number"];
        
        NSLog(@"%@,,%@",payVC.str,payVC.number);
        
        [self.navigationController pushViewController:payVC animated:YES];
    }
}

- (void)refundNotification:(NSNotification *)text{
    
    if ([text.userInfo[@"selected"] isEqualToString:@"yes"]) {
        
        [self requestData];
    }
}

#pragma mark -------------------- tableView ----------------------

#pragma mark --- UITableViewDelegate ---

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_dataArray[indexPath.row][@"status"]intValue] == 2) {
        
        return screenScale(320);
    }
    
    return screenScale(410);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"1");
}

#pragma mark --- UITableViewDataSource ---

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_dataArray[indexPath.row][@"status"]intValue] == 1) {
        
        [tableView registerClass:[threeTableViewCell class] forCellReuseIdentifier:@"threeTableViewCell"];
        threeTableViewCell *cell = (threeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"threeTableViewCell" forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.stateLable.text = _dataArray[indexPath.row][@"status_info"];
        cell.dateLable.text = _dataArray[indexPath.row][@"add_time"];
        cell.nameTextView.text = _dataArray[indexPath.row][@"goods_name"];
        cell.priceLable.text = [@"¥" stringByAppendingString:_dataArray[indexPath.row][@"price"]];
        cell.numLable.text = [@"x" stringByAppendingString:_dataArray[indexPath.row][@"num"]];
        cell.moneyLable.text = [@"总计：" stringByAppendingString:[NSString stringWithFormat:@"%@",_dataArray[indexPath.row][@"total"]]];
        cell.codeLable.text = [@"识别码：" stringByAppendingString:_dataArray[indexPath.row][@"code"]];
        
        cell.orderId = _dataArray[indexPath.row][@"id"];
        cell.index = [NSString stringWithFormat:@"%ld",indexPath.row];
        
        NSString *urlGood = _dataArray[indexPath.row][@"goods_pic"];
        NSString *urlN = manage(AP_HOST, urlGood);
        NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
        NSURL *urlW = [NSURL URLWithString:strUrl];
        [cell.logoImg sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
        
        return cell;
    }else if ([_dataArray[indexPath.row][@"status"]intValue] == 2){
    
        [tableView registerClass:[fourTableViewCell class] forCellReuseIdentifier:@"fourTableViewCell"];
        fourTableViewCell *cell = (fourTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"fourTableViewCell" forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.stateLable.text = _dataArray[indexPath.row][@"status_info"];
        cell.dateLable.text = _dataArray[indexPath.row][@"add_time"];
        cell.nameTextView.text = _dataArray[indexPath.row][@"goods_name"];
        cell.priceLable.text = [@"¥" stringByAppendingString:_dataArray[indexPath.row][@"price"]];
        cell.numLable.text = [@"x" stringByAppendingString:_dataArray[indexPath.row][@"num"]];
        cell.moneyLable.text = [@"总计：" stringByAppendingString:[NSString stringWithFormat:@"%@",_dataArray[indexPath.row][@"total"]]];
        
        cell.orderId = _dataArray[indexPath.row][@"id"];
        cell.index = [NSString stringWithFormat:@"%ld",indexPath.row];
        
        NSString *urlGood = _dataArray[indexPath.row][@"goods_pic"];
        NSString *urlN = manage(AP_HOST, urlGood);
        NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
        NSURL *urlW = [NSURL URLWithString:strUrl];
        [cell.logoImg sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
        
        return cell;
    }
    
    [tableView registerClass:[twoTableViewCell class] forCellReuseIdentifier:@"twoTableViewCell"];
    twoTableViewCell *cell = (twoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"twoTableViewCell" forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.stateLable.text = _dataArray[indexPath.row][@"status_info"];
    cell.dateLable.text = _dataArray[indexPath.row][@"add_time"];
    cell.nameTextView.text = _dataArray[indexPath.row][@"goods_name"];
    cell.priceLable.text = [@"¥" stringByAppendingString:_dataArray[indexPath.row][@"price"]];
    cell.numLable.text = [@"x" stringByAppendingString:_dataArray[indexPath.row][@"num"]];
    cell.moneyLable.text = [@"总计：" stringByAppendingString:[NSString stringWithFormat:@"%@",_dataArray[indexPath.row][@"total"]]];
    
    cell.orderId = _dataArray[indexPath.row][@"id"];
    cell.index = [NSString stringWithFormat:@"%ld",indexPath.row];
    cell.mark = @"oneVC";
    
    NSString *urlGood = _dataArray[indexPath.row][@"goods_pic"];
    NSString *urlN = manage(AP_HOST, urlGood);
    NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
    NSURL *urlW = [NSURL URLWithString:strUrl];
    [cell.logoImg sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
    
    return cell;
}

@end
